﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.actions {
    import Engine.Helpers.*;
    import Classes.*;
    import Classes.sim.*;
    import Engine.*;

    public class ActionNavigateRandom extends ActionNavigate {

        protected static const MAX_LOOKAHEAD:int = 5;

        protected var m_elapsed:Number = 0;
        protected var m_timeout:Number = -1;

        public function ActionNavigateRandom(_arg1:NPC){
            var _local3:Road;
            var _local4:Vector3;
            var _local5:Vector3;
            super(_arg1, null, null);
            var _local2:Vector3 = _arg1.getPosition();
            if ((((_local2 == null)) || ((((_local2.x == 0)) && ((_local2.y == 0)))))){
                _local3 = Global.world.citySim.roadManager.findRandomRoad();
                if (_local3 != null){
                    _local2 = _local3.getPosition();
                    m_npc.setPosition(_local2.x, _local2.y);
                    this.updatePath(false);
                    if ((((m_path == null)) || ((m_path.length == 0)))){
                        _local4 = _local3.getHotspot();
                        m_npc.setPosition(_local4.x, _local4.y);
                    } else {
                        _local5 = (m_path[0] as PathElement).offsetPosition;
                        m_npc.setPosition(_local5.x, _local5.y);
                    };
                };
            };
        }
        public function setTimeout(_arg1:Number):ActionNavigateRandom{
            this.m_timeout = _arg1;
            return (this);
        }
        override public function getInterrupt():int{
            return (FULL_INTERRUPT);
        }
        override public function enter():void{
            this.m_elapsed = 0;
        }
        override public function reenter():void{
            this.m_elapsed = 0;
        }
        override public function update(_arg1:Number):void{
            if ((((m_path == null)) || ((m_path.length < MAX_LOOKAHEAD)))){
                this.updatePath(true);
            };
            super.update(_arg1);
            this.m_elapsed = (this.m_elapsed + _arg1);
            if ((((this.m_timeout >= 0)) && ((this.m_elapsed > this.m_timeout)))){
                this.m_npc.actionQueue.removeState(this);
            };
        }
        override protected function get navigationVelocity():Number{
            return (m_npc.velocityWalk);
        }
        override protected function updatePath(_arg1:Boolean):void{
            var _local3:Road;
            var _local4:Road;
            var _local5:Vector3;
            var _local6:Vector3;
            var _local7:Array;
            if (((m_path) && ((m_path.length >= MAX_LOOKAHEAD)))){
                return;
            };
            var _local2:Vector3 = m_npc.getPosition();
            if (((isNaN(_local2.x)) || (isNaN(_local2.y)))){
                _local3 = Global.world.citySim.roadManager.findRandomRoad();
                if (_local3 != null){
                    _local2 = _local3.getPosition();
                    m_npc.setPosition(_local2.x, _local2.y);
                } else {
                    m_npc.setPosition(0, 0);
                };
            };
            if (((!(m_path)) || ((m_path.length == 0)))){
                _local4 = Global.world.citySim.roadManager.findClosestRoad(_local2);
                _local5 = ((_local4)!=null) ? _local4.getHotspot() : _local2;
                _local6 = this.getAxisAlignedDirection(_local2, _local5);
                _local7 = [_local5];
            } else {
                _local6 = this.getAxisAlignedDirection(_local2, (m_path[0] as PathElement).offsetPosition);
                _local7 = m_path.map(PathElement.extractBasePosition);
            };
            this.extendRoadPositions(_local7);
            _local7 = _local7.map(PathElement.createInstance);
            if (Global.world.citySim.roadManager.isShowingOverlay){
                m_npc.m_navDbgPath2 = _local7;
                m_npc.m_navRandom = true;
            };
            m_path = makeOffsetPath(_local7, _local6, false);
            debugPrintPath();
        }
        protected function extendRoadPositions(_arg1:Array):void{
            var _local2:Vector3;
            var _local3:Vector3;
            var _local4:Vector3;
            while (_arg1.length < MAX_LOOKAHEAD) {
                _local2 = ((_arg1.length)>1) ? _arg1[(_arg1.length - 2)] : null;
                _local3 = _arg1[(_arg1.length - 1)];
                _local4 = this.findNextRoadPosition(_local2, _local3);
                _arg1.push(((_local4)!=null) ? _local4 : _local3);
            };
        }
        protected function findNextRoadPosition(_arg1:Vector3, _arg2:Vector3):Vector3{
            var _local4:Vector3;
            var _local5:Vector3;
            var _local6:Vector3;
            var _local7:Vector3;
            var _local10:Road;
            var _local11:Vector3;
            var _local12:Vector3;
            var _local13:Vector3;
            var _local14:Vector3;
            var _local15:Number;
            var _local16:Number;
            var _local3:Array = Global.world.citySim.roadManager.findClosestRoadNeighbors(_arg2);
            var _local8:Vector3 = this.getAxisAlignedDirection(_arg1, _arg2);
            var _local9:Vector3 = _local8.cross(V_UP).normalize();
            for each (_local10 in _local3) {
                if (_local10 == null){
                } else {
                    _local13 = _local10.getHotspot();
                    _local14 = _local13.subtract(_arg2);
                    _local15 = _local8.dot(_local14);
                    if (_local15 > 0){
                        _local6 = _local13;
                    } else {
                        if (_local15 < 0){
                            _local7 = _local13;
                        } else {
                            _local16 = _local9.dot(_local14);
                            if (_local16 > 0){
                                _local4 = _local13;
                            } else {
                                if (_local16 < 0){
                                    _local5 = _local13;
                                };
                            };
                        };
                    };
                };
            };
            _local11 = new Vector3(-(m_roadSize), -(m_roadSize));
            _local12 = this.chooseNextTile(_local4, _local5, _local6, _local7);
            return (_local12);
        }
        protected function getAxisAlignedDirection(_arg1:Vector3, _arg2:Vector3):Vector3{
            var _local3:Vector3 = (((!((_arg1 == null))) && (!((_arg2 == null))))) ? _arg2.subtract(_arg1) : new Vector3(1, 0);
            if (Math.abs(_local3.x) > Math.abs(_local3.y)){
                _local3.y = 0;
            } else {
                _local3.x = 0;
            };
            if (_local3.length() > 0){
                return (_local3.normalize());
            };
            return (_local3);
        }
        protected function findRoadAt(_arg1:Vector3, _arg2:Number=0.1):Vector3{
            var _local5:*;
            var _local3:Vector3 = _arg1.floor();
            var _local4:Array = Global.world.getCollisionMap().getObjectsByPosition(_local3.x, _local3.y);
            for each (_local5 in _local4) {
                if ((_local5 is Road)){
                    return ((_local5 as Road).getPosition());
                };
            };
            return (null);
        }
        protected function chooseNextTile(_arg1:Vector3, _arg2:Vector3, _arg3:Vector3, _arg4:Vector3):Vector3{
            var _local5:Array = [];
            var _local6:Array = [];
            var _local7:Boolean = ((((((!((_arg1 == null))) && (!((_arg2 == null))))) && (!((_arg3 == null))))) && (!((_arg4 == null))));
            if (_arg1){
                _local5.push(_arg1);
                _local6.push(1);
            };
            if (_arg3){
                _local5.push(_arg3);
                _local6.push((_local7) ? 1 : 0.5);
            };
            if (_arg2){
                _local5.push(_arg2);
                _local6.push((_local7) ? 1 : 0.5);
            };
            if (_arg4){
                _local5.push(_arg4);
                _local6.push(1E-5);
            };
            var _local8:int = MathUtil.randomIndexWeighed(_local6);
            return ((_local5[_local8] as Vector3));
        }

    }
}//package Classes.actions 
